package com.bio.action;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.bio.entity.BaseObject;
import com.bio.entity.attachemnt.Attachment;
import com.bio.exception.OperationException;
import com.bio.service.Service;
import com.bio.utils.Pager;
import com.bio.utils.Pagination;
import com.opensymphony.xwork2.ActionSupport;

public class AbstractAction<E extends Serializable, T extends Service<E>> extends ActionSupport implements Serializable{

    private static final long serialVersionUID = 1989785264574538957L;
    
    protected final static String VIEW = "view";
    
    protected final static String INPUT = "input";
    
    protected final static String LIST = "list";
    
    protected final static String EDIT = "edit";
    
    protected final static String INDEX = "index";
    
    protected Integer currentIndex = 0;

    protected Long id;
    
    protected Long[] ids;
    
    protected T service;
    
    protected E model;
    
    protected Pager<E> pager = Pager.EMPTY_PAGE;
    
    protected Pagination pagination = new Pagination();
    
    protected String message ;
    
    protected List<Attachment> attachments;
    
	public com.bio.exception.Result operateResult;
    
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public AbstractAction(T service) {
        this.service = service;
    }
    
    public String get() {
        this.model = this.service.get(this.id);
        return Dispatcher.MULTFORMAT.dispatch(VIEW);
    }
    
    public String list() {
        if (this.currentIndex == null) {
            this.currentIndex = 0;
        }
        this.pagination.setCurrentIndex(this.currentIndex);
        this.pager = this.service.findAllByPager(pagination);
        return Dispatcher.MULTFORMAT.dispatch(LIST);
    }
    
    public String listForAdmin() {
        if (this.currentIndex == null) {
            this.currentIndex = 0;
        }
        this.pagination.setCurrentIndex(this.currentIndex);
        this.pager = this.service.findAllByPagerNoCache(pagination);
        return Dispatcher.MULTFORMAT.dispatch(LIST);
    }
    
    public String edit() {
        if (this.id != null) {
            this.model = this.service.get(this.id);
        } 
        return Dispatcher.MULTFORMAT.dispatch(INPUT);
    }

    private final static String[] INGORED_PROPERTIES = {"id", "createdDate", "creator", "updatedDate", "updater", "deletedDate", "deleter", "isDeleted"};
    
    public String save() throws OperationException {
        if (this.model instanceof BaseObject && ((BaseObject) this.model).getId() != null) {
            E existedModel = this.service.get(((BaseObject) this.model).getId());
            if (existedModel != null) {
                BeanUtils.copyProperties(this.model, existedModel, INGORED_PROPERTIES);
                this.model = existedModel;
            } else {
                ((BaseObject) this.model).setId(null);
                ((BaseObject) this.model).setDeleted(BaseObject.FLAG_UNDELETED);
            }
        }
        this.service.save(this.model);
        return Dispatcher.MULTFORMAT.dispatch(VIEW);
    }
    
    public String remove() throws OperationException {
        this.service.remove(this.id);
        return Dispatcher.MULTFORMAT.dispatch(LIST);
    }
    
    public String deleteAll(){
    	for(Long id : ids){
    		this.service.remove(id);
    	}
    	return "tolist";
    }
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public E getModel() {
        return model;
    }

    public void setModel(E model) {
        this.model = model;
    }

    public Pager<E> getPager() {
        return pager;
    }

    public void setPager(Pager<E> pager) {
        this.pager = pager;
    }

    public Pagination getPagination() {
        return pagination;
    }

    public void setPagination(Pagination pagination) {
        this.pagination = pagination;
    }

    public Integer getCurrentIndex() {
        return currentIndex;
    }

    public void setCurrentIndex(Integer currentIndex) {
        this.currentIndex = currentIndex;
    }

    protected String dispatch(String result) {
        return Dispatcher.MULTFORMAT.dispatch(result);
    }

	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}

	public List<Attachment> getAttachments() {
		return attachments;
	}

	public void setAttachments(List<Attachment> attachments) {
		this.attachments = attachments;
	}
    
}
